package com.pureut.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.domain.DocumentManagement;
import com.pureut.system.domain.vo.DocumentManagementVo;
import com.pureut.system.mapper.DocumentManagementMapper;
import com.pureut.system.service.IDocumentManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Author: pureut
 * @Date: 2024-12-06 13:51:19
 * @Desc: 文档管理
 */
@Service("documentManagementService")
public class DocumentManagementServiceImpl extends ServiceImpl<DocumentManagementMapper, DocumentManagement> implements IDocumentManagementService {

    /**
     * 文档管理Dao
     */
    @Autowired
    private DocumentManagementMapper documentManagementDao;

    /**
     * 查询列表
     *
     * @param documentManagementVo
     * @return
     */
    @Override
    public List<DocumentManagement> queryPage(DocumentManagementVo documentManagementVo) {
        // 获取认证方式的数据字典
        List<SysDictData> netArray = DictUtils.getDictCache("file_type");
        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        // 获取认证方式的数据字典
        List<SysDictData> netArray2 = DictUtils.getDictCache("file_staus");
        Map<String, String> netMap2 = netArray2.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<DocumentManagement> documentManagements = documentManagementDao.selectByPage(documentManagementVo);
        documentManagements.forEach(e->{
            e.setFileClassId(netMap.get(String.valueOf(e.getFileClassId())));
            e.setStatus(netMap2.get(String.valueOf(e.getStatus())));
        });
        return documentManagements;
    }


    /**
     * 新增文档管理
     *
     * @param documentManagementVo
     * @return
     */
    @Override
    public Boolean save(DocumentManagementVo documentManagementVo) {
        DocumentManagement documentManagement = new DocumentManagement();
        BeanUtil.copyProperties(documentManagementVo, documentManagement);
        documentManagement.setCreateBy(SecurityUtils.getUsername());
        documentManagement.setCreateTime(new Date());
        documentManagement.setStatus("1");
        return save(documentManagement);
    }


    /**
     * 更新文档管理
     *
     * @param documentManagementVo
     * @return
     */
    @Override
    public Boolean updateById(DocumentManagementVo documentManagementVo) {
        DocumentManagement documentManagement = new DocumentManagement();
        BeanUtil.copyProperties(documentManagementVo, documentManagement);
        documentManagement.setUpdateBy(SecurityUtils.getUsername());
        documentManagement.setUpdateTime(new Date());
        return updateById(documentManagement);
    }

    /**
     * 审核
     * @param documentManagementVo
     * @return
     */
    @Override
    public Boolean examine(DocumentManagementVo documentManagementVo){
        DocumentManagement documentManagement = new DocumentManagement();
        BeanUtil.copyProperties(documentManagementVo, documentManagement);
        documentManagement.setUpdateBy(SecurityUtils.getUsername());
        documentManagement.setUpdateTime(new Date());
        documentManagement.setStatus("2");
        return updateById(documentManagement);
    }
}
